﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using DocManagerORM.Utils;
using System.Collections;
using System.Data.Objects;


namespace DocManagerORM.ORM
{
    [Serializable]
    public class QueryCodifiche:QueryBase 
    {

        static Func<DocManagerDbEntities, IQueryable<Categorie>> queryCategorieAllCompiled = null;
        static Func<DocManagerDbEntities, int, IQueryable<Categorie>> queryCategorieByIdCompiled = null;
        static Func<DocManagerDbEntities, int?, IQueryable<Categorie>> querySottoCategorieCompiled = null;

        public static void LoadCacheCategorie()
        {
                        
        }//fM


        public IQueryable<Categorie> getCATEGORIE(int? pId)
        {
            if (queryCategorieAllCompiled == null)
            {
                queryCategorieAllCompiled = CompiledQuery.Compile<DocManagerDbEntities, IQueryable<Categorie>>(
                         (ctx) => ctx.Categorie.OrderBy(s => s.Categoria)                      
               );
            }

            if (queryCategorieByIdCompiled == null)
            {
                queryCategorieByIdCompiled = CompiledQuery.Compile<DocManagerDbEntities, int, IQueryable<Categorie>>(
                         (ctx, pID) => 
                             ctx.Categorie.Where(c => c.ID == pID).OrderBy(s => s.Categoria)
               );
            }

            if (pId.HasValue == null)
                return queryCategorieByIdCompiled.Invoke(m_Entity, pId.Value);
            else
                return queryCategorieAllCompiled.Invoke(m_Entity); 
            
                  
        }//fM

        public IQueryable<SottoCategorie> getSottoCATEGORIE(int? pIDPadre)
        {          
           if (pIDPadre == null)
                return m_Entity.SottoCategorie.OrderBy(s => s.Categoria);
            else
                return  m_Entity.SottoCategorie.Where(c => c.Fk_IDCat == pIDPadre.Value)
                    .OrderBy(s=> s.Categoria);
        }

        public BindingListExt<Categorie> getCATEGORIEbl(int? pID)
        {
            var listRet = m_Entity.Categorie.Where(c => c.ID == pID.Value).ToList() ;
            var bl = new BindingListExt<Categorie>(listRet,ref m_Entity);
            return bl;
        }


        public BindingListExt<SottoCategorie> getSottoCATEGORIEbl(int pIDpadre)
        {
            var listRet = m_Entity.SottoCategorie.Where(c => c.Fk_IDCat == pIDpadre).ToList();
            var bl = new BindingListExt<SottoCategorie>(listRet, ref m_Entity);
            return bl;
        }

        public IQueryable<Tags> getTagsPredefiniti(int? pID)
        {
            if (pID == null)
                return m_Entity.Tags.OrderBy(s => s.TagsPre);
            else
                return m_Entity.Tags.Where(c => c.ID == pID.Value).OrderBy(s => s.TagsPre);
        }


        public IQueryable<AttivitaIter> getAttivitaIter(int pTipo)
        {
            var listRet = m_Entity.AttivitaIter.Where(x=> x.Tipo == pTipo) ;
            return listRet;
        }


        public IBindingList getTutteTabelleDati(string pTabella,bool pConDAtiValidazione)
        {
            IList lista;
            IBindingList listaN;

            var discover = new DS2171Utils.ObjectDiscover()   ;
            var campiList = discover.getCampiClasseOggetto(pTabella, "DocManagerORM.ORM", "DocManagerORM.dll");
            var campoTipo = campiList.Item2; //campo e tipo per fare un controllo

            switch (pTabella)
            {
                case "aspnet_Applications":
                    var lista0 = m_Entity.aspnet_Applications.ToList();
                    return new BindingListExt<aspnet_Applications>(lista0, ref m_Entity ,campoTipo);
                    break;
                case "aspnet_Membership":
                    var lista1 = m_Entity.aspnet_Membership.ToList();
                    return new BindingListExt<aspnet_Membership>(lista1, ref m_Entity, campoTipo);
                    break;
                case "aspnet_Paths":
                    var lista2 = m_Entity.aspnet_Paths.ToList();
                    return new BindingListExt<aspnet_Paths>(lista2, ref m_Entity, campoTipo);
                    break;
                case "aspnet_PersonalizationAllUsers":
                    var lista3 = m_Entity.aspnet_PersonalizationAllUsers.ToList();
                    return new BindingListExt<aspnet_PersonalizationAllUsers>(lista3, ref m_Entity, campoTipo);
                    break;
                case "aspnet_PersonalizationPerUser":
                    var lista4 = m_Entity.aspnet_PersonalizationPerUser.ToList();
                    return new BindingListExt<aspnet_PersonalizationPerUser>(lista4, ref m_Entity, campoTipo);
                    break;
                case "aspnet_Profile":
                    var lista5 = m_Entity.aspnet_Profile.ToList();
                    return new BindingListExt<aspnet_Profile>(lista5, ref m_Entity, campoTipo);
                    break;
                case "aspnet_Roles":
                    var lista6 = m_Entity.aspnet_Roles.ToList();
                    return new BindingListExt<aspnet_Roles>(lista6, ref m_Entity, campoTipo);
                    break;
                case "aspnet_SchemaVersions":
                    var lista7 = m_Entity.aspnet_SchemaVersions.ToList();
                    return new BindingListExt<aspnet_SchemaVersions>(lista7, ref m_Entity, campoTipo);
                    break;
                case "aspnet_Users":
                    var lista8 = m_Entity.aspnet_Users.ToList();
                    return new BindingListExt<aspnet_Users>(lista8, ref m_Entity, campoTipo);
                    break;
                case "aspnet_WebEvent_Events":
                    var lista9 = m_Entity.aspnet_WebEvent_Events.ToList();
                    return new BindingListExt<aspnet_WebEvent_Events>(lista9, ref m_Entity, campoTipo);
                    break;
                case "AttivitaIter":
                    var lista10 = m_Entity.AttivitaIter.ToList();
                    return new BindingListExt<AttivitaIter>(lista10, ref m_Entity,campoTipo);
                    break;
                case "CanaliFeed":
                    var lista11 = m_Entity.CanaliFeed.ToList();
                    return new BindingListExt<CanaliFeed>(lista11, ref m_Entity,campoTipo);
                    break;
                case "Categorie":
                    var lista12 = m_Entity.Categorie.ToList();
                    return new BindingListExt<Categorie>(lista12, ref m_Entity,campoTipo);
                    break;
                case "CategorieFeed":
                    var lista13 = m_Entity.CategorieFeed.ToList();
                    return new BindingListExt<CategorieFeed>(lista13, ref m_Entity,campoTipo);
                    break;
                case "Documenti":
                    var lista14 = m_Entity.Documenti.ToList();
                    return new BindingListExt<Documenti>( ref m_Entity,campoTipo,true);
                    break;
                case "Iters":
                    var lista15 = m_Entity.Iters.ToList();
                    return new BindingListExt<Iters>(lista15, ref m_Entity,campoTipo);
                    break;
                case "ItersManager":
                    var lista16 = m_Entity.ItersManager.ToList();
                    return new BindingListExt<ItersManager>(lista16, ref m_Entity,campoTipo);
                    break;
                case "SettingsApplicazione":
                    var lista17 = m_Entity.SettingsApplicazione.ToList();
                    return new BindingListExt<SettingsApplicazione>(lista17, ref m_Entity,campoTipo);
                    break;
                case "SettingsBaseApplicazione":
                    var lista18 = m_Entity.SettingsBaseApplicazione.ToList();
                    return new BindingListExt<SettingsBaseApplicazione>(lista18, ref m_Entity,campoTipo);
                    break;
                case "SottoCategorie":
                    var lista19 = m_Entity.SottoCategorie.ToList();
                    return new BindingListExt<SottoCategorie>(lista19, ref m_Entity,campoTipo);
                    break;
                case "Tags":
                    var lista20 = m_Entity.Tags.ToList();
                    return new BindingListExt<Tags>(lista20, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_Applications":
                    var lista21 = m_Entity.vw_aspnet_Applications.ToList();
                    return new BindingListExt<vw_aspnet_Applications>(lista21, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_MembershipUsers":
                    var lista22 = m_Entity.vw_aspnet_MembershipUsers.ToList();
                    return new BindingListExt<vw_aspnet_MembershipUsers>(lista22, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_Profiles":
                    var lista23 = m_Entity.vw_aspnet_Profiles.ToList();
                    return new BindingListExt<vw_aspnet_Profiles>(lista23, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_Roles":
                    var lista24 = m_Entity.vw_aspnet_Roles.ToList();
                    return new BindingListExt<vw_aspnet_Roles>(lista24, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_Users":
                    var lista25 = m_Entity.vw_aspnet_Users.ToList();
                    return new BindingListExt<vw_aspnet_Users>(lista25, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_UsersInRoles":
                    var lista26 = m_Entity.vw_aspnet_UsersInRoles.ToList();
                    return new BindingListExt<vw_aspnet_UsersInRoles>(lista26, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_WebPartState_Paths":
                    var lista27 = m_Entity.vw_aspnet_WebPartState_Paths.ToList();
                    return new BindingListExt<vw_aspnet_WebPartState_Paths>(lista27, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_WebPartState_Shared":
                    var lista28 = m_Entity.vw_aspnet_WebPartState_Shared.ToList();
                    return new BindingListExt<vw_aspnet_WebPartState_Shared>(lista28, ref m_Entity,campoTipo);
                    break;
                case "vw_aspnet_WebPartState_User":
                    var lista29 = m_Entity.vw_aspnet_WebPartState_User.ToList();
                    return new BindingListExt<vw_aspnet_WebPartState_User>(lista29, ref m_Entity,campoTipo);
                    break;
                case "vwDocumenti":
                    var lista30 = m_Entity.vwDocumenti.ToList();
                    return new BindingListExt<vwDocumenti>(lista30, ref m_Entity,campoTipo);
                    break;
                case "vwMembershipSimple":
                    var lista31 = m_Entity.vwMembershipSimple.ToList();
                    return new BindingListExt<vwMembershipSimple>(lista31, ref m_Entity,campoTipo);
                    break;

                default:
                    return null;
                    break;
            }

           

        }//fM


          
    }//fC
}
